package finelProject.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

import finelProject.MyAppConstants;

public class User {
	private String 	UserName;
	private String	Password;
	private String 	NickName;
	private String	ShortDescription;
	private String	PhotoURL;
	private int		Followers;
	private int 	Folloews;
	
	public User(String SetUserName, String SetPassword,String SetNickName, String SetShortDescription, String SetPhotoURL) {
		UserName = SetUserName;
		Password = SetPassword;
		NickName = SetNickName;
		ShortDescription = SetShortDescription;
		PhotoURL = SetPhotoURL;
	}
	
	
	public String getUserName(){
		return UserName;
	}
	public String getPassword(){
		return Password;
	}
	public String getNickName(){
		return NickName;
	}
	public String getShortDescription(){
		return ShortDescription;
	}
	public String getPhotoURL(){
		return PhotoURL;
	}
	public int getfollow(){
	Context context;
	try {
		context = new InitialContext();
		BasicDataSource ds = (BasicDataSource)context.lookup(MyAppConstants.DB_DATASOURCE);
		Connection conn = ds.getConnection();
		PreparedStatement stmt;
		try {
			stmt = conn.prepareStatement(MyAppConstants.SUM_NUMBER_OF_FOLLOWERS);
			stmt.setString(1,UserName);
			ResultSet rs = stmt.executeQuery();
			if(rs.next()){
				Followers = rs.getInt(1);
			}
			stmt = conn.prepareStatement(MyAppConstants.SUM_NUMBER_OF_FOLLOWES);
			stmt.setString(1,UserName);
			rs = stmt.executeQuery();
			if(rs.next()){
				Followers = rs.getInt(1);
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			//TODO: need to handle the exception.
		}
		context.close();

	} catch (NamingException | SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	return Followers;
	}
}

